package com.baiduspring.function.elasticsearch.operation;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtils {
    public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_FORMAT = "yyyy-MM-dd";

    private static final String DATE_FORMAT2 = "yyyyMMdd";

    public static String timeFormat(Date date) {
        DateFormat dateFormat = new SimpleDateFormat(TIME_FORMAT);
        return dateFormat.format(date);
    }

    public static Date timeToDate(String date) {
        DateFormat dateFormat = new SimpleDateFormat(TIME_FORMAT);
        try {
            return dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String dateFormat(Date date) {
        DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        return dateFormat.format(date);
    }

    public static Date dateFormat(String date) {
        DateFormat dateformat = new SimpleDateFormat(DATE_FORMAT);
        try {
            return dateformat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date datetimeFormat(String date) {
        DateFormat dateformat = new SimpleDateFormat(TIME_FORMAT);
        try {
            return dateformat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date toDate(String time) {
        DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            return dateformat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date toDate(String time, String format) {
        DateFormat dateformat = new SimpleDateFormat(format);
        try {
            return dateformat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将当前时间格式化为：yyyy-MM-dd HH:mm:ss格式，再提取出其中的数字，组成数字日期格式。
     * 比如：2017-03-13 18:18:32，结果为20170313181832
     *
     * @return
     */
    public static String currentTimeNumberStr() {
        DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateformat.format(new Date());

        String regEx = "[^0-9]";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(date);
        String str = matcher.replaceAll("").trim();
        return str;
    }

    public static String stringdateFormat(String date) {
        //String date = "Mon Mar 02 13:57:49 CST 2015";
        SimpleDateFormat sdf1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
        try{
        	Date d=sdf1.parse(date);
            SimpleDateFormat sdf=new SimpleDateFormat(DATE_FORMAT);
            return sdf.format(d);
        }catch (ParseException e){
            e.printStackTrace();
        }
		return null;
    }
}